package logic

import (
	"beike/api/internal/svc"
	"beike/api/internal/types"
	"beike/houserpc/houserpc"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewHouseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseAddLogic {
	return &HouseAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *HouseAddLogic) HouseAdd(req *types.HouseAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	if req.Title == "" || req.Intro == "" || req.Price == 0 || req.Address == "" {
		return &types.Response{
			Message: "房源信息还未完善，请在完善后上传",
			Data:    nil,
			Code:    0,
		}, nil
	}

	add, err := l.svcCtx.HouseRpc.HouseAdd(l.ctx, &houserpc.HouseAddRequest{
		UserId:  req.UserId,
		Title:   req.Title,
		Intro:   req.Intro,
		Price:   float32(req.Price),
		Address: req.Address,
		Types:   req.Types,
		Area:    float32(req.Area),
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    0,
		}, nil
	}

	return &types.Response{
		Message: "房源信息上传成功",
		Data:    add,
		Code:    200,
	}, nil
}
